;	Copyright (C) 2009 Salil Bhagurkar
;	
;	This file is part of ViSE
;	
;	ViSE is free software: you can redistribute it and/or modify
;	it under the terms of the GNU Lesser General Public License as published by
;	the Free Software Foundation, either version 3 of the License, or
;	(at your option) any later version.
;	
;	ViSE is distributed in the hope that it will be useful,
;	but WITHOUT ANY WARRANTY; without even the implied warranty of
;	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;	GNU Lesser General Public License for more details.
;	
;	You should have received a copy of the GNU Lesser General Public License
;	along with ViSE.  If not, see <http://www.gnu.org/licenses/>.

[bits 32]
;Task management code:
extern _debug

extern _end_of_task

global _task_return
_task_return:
call _end_of_task
jmp $
ret

toeip dd 0

global _arch_resume_task;(void stack)
_arch_resume_task:
	mov esp,[esp+4]
	pop dword[toeip]
	popf
	pop ebp
	pop edi
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	jmp dword[toeip]
ret ;)



global _arch_start_task;(void stack)
_arch_start_task:
	mov esp,[esp+4]
	pop eax  ;This is the entry address of the function
	sti  ;Make sure interrupts are enabled
	call eax  ;Call the task
	jmp _task_return
	jmp $
ret


global _arch_switch_task;(u32 stack,u32 *srcstack)
_arch_switch_task:
	push eax
	push ebx
	push ecx
	push edx
	push esi
	push edi
	push ebp
	pushf
	push .resume_point
	mov eax,[esp+44] ;Get the source stack pointer
	mov [eax],esp  ;Save current esp to the stack pointer location
	mov esp,[esp+40]
	pop dword[toeip]
	popf
	pop ebp
	pop edi
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	jmp dword[toeip]
	.resume_point:
	mov eax,0
ret


msg db "Stack is %x", 10, 0

global _arch_switch_task_first;(u32 stack,u32 *srcstack)
_arch_switch_task_first:
	push eax
	push ebx
	push ecx
	push edx
	push esi
	push edi
	push ebp
	pushf
	push .resume_point
	mov eax,[esp+44] ;Get the source stack pointer
	mov [eax],esp ;Save current esp to the stack pointer location
	mov esp,[esp+40]
	pop eax  ;This is the entry address of the function
	sti  ;Make sure interrupts are enabled
	call eax  ;Call the task
	jmp _task_return
	jmp $
	.resume_point:
ret
